"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 备节点执行备份，-h指定主节点，备节点build
Create At   : 2020/11/27
@zou_jialiang0505328126
Description :
    1.备机执行备份指令-h指定主节点
    2.备机备份的同时重建
    3.使用备份文件恢复主机
    4.重启集群
    5.执行tpcc
Expect      :
    1.备份成功
    2.重建成功
    3.恢复成功
    4.重启集群成功
    5.执行tpcc成功
History     :
    Modified by @wan005 2021/8/16:增加备份结果查询
"""

import os
import unittest
import sys
import time
from yat.test import macro
from yat.test import Node

sys.path.append(sys.path[0] + "/../")
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.ComThread import ComThread


class Basebackup(unittest.TestCase):
    commonshpri = CommonSH('PrimaryDbUser')
    commonshsta = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Tools_Backup_Case009.py 开始执行------------')
        self.dbPrimaryDbUser = Node(node='PrimaryDbUser')
        self.dbStandby1DbUser = Node(node='Standby1DbUser')
        self.dbStandby2DbUser = Node(node='Standby2DbUser')
        self.dbStandby1Root = Node(node='Standby1Root')
        self.dbPrimaryRootNode = Node(node='PrimaryRoot')
        self.Constant = Constant()
        self.common = Common('PrimaryRoot')
        self.backup_file = os.path.join(macro.DB_BACKUP_PATH, 'backup_dir')
        self.standby_hostname = self.dbStandby1DbUser.sh('hostname').result()
        self.primary_hostname = self.dbPrimaryDbUser.sh('hostname').result()

        self.log.info('-------------------创建备份路径-----------------------------------')
        shell_cmd = f'rm -rf {self.backup_file}; mkdir {self.backup_file};'
        self.log.info(shell_cmd)
        result = self.dbPrimaryDbUser.sh(shell_cmd).result()
        self.log.info(result)

    def test_backup(self):
        self.log.info("---------------1.备机执行备份指令-h指定主节点：成功---------------------")
        backup_thread = ComThread(self.commonshsta.execu_gs_backup,
                                  args=('backup', self.primary_hostname, self.backup_file, "--all",))
        backup_thread.setDaemon(True)
        backup_thread.start()

        self.log.info('--------------------2.主机备份的同时备机重建：成功----------------------------')
        text = '----step2：主机备份的同时备机重建 expect:成功----'
        result = self.commonshsta.buildStandby(mode='')
        self.assertIn(self.Constant.BUILD_SUCCESS_MSG, result, '执行失败:' + text)

        self.log.info("============检查步骤1结果====================")
        text = '----step1：备机执行备份指令-h指定主节点 expect:成功----'
        backup_thread.join(60 * 60)
        result_backup = backup_thread.get_result()
        self.log.info(result)
        self.assertIn(self.Constant.BACKUP_BACKUP_SUCCESS_MSG,
                      result_backup, '执行失败:' + text)

        cmd = f'ls -l {self.backup_file} |grep "^-"|wc -l'
        self.log.info(cmd)
        result = self.dbPrimaryDbUser.sh(cmd).result()
        self.log.info(result)
        self.assertLess(0, int(result), '执行失败:' + text)

        self.log.info('-------------------3.主节点使用备份文件恢复备机:成功-----------------------------------')
        text = '----step3：主节点使用备份文件恢复备机 expect:成功----'
        result = self.commonshpri.execu_gs_backup('restore', self.primary_hostname, self.backup_file, "--all")
        self.assertIn(self.Constant.BACKUP_RESTORE_SUCCESS_MSG, result,
                      '执行失败:' + text)

        self.log.info('-------------------4.重启集群:成功------------------------')
        text = '----step4：重启集群 expect:成功----'
        result1 = self.commonshpri.stopDbCluster()
        result2 = self.commonshpri.startDbCluster()
        self.assertTrue(result1 and result2, '执行失败:' + text)

        self.log.info('-----------------------------5.执行TPCC----------------------------------')
        text = '----step4：执行TPCC expect:成功----'
        result = self.common.startTPCC(self.dbPrimaryRootNode, macro.TPCC_PATH)
        self.log.info(result)
        self.assertIn(self.Constant.TPCC_SUCCESS_MSG, result,
                        '执行失败:' + text)

    def tearDown(self):
        self.log.info('-------------------------环境清理------------------------')
        self.log.info('--------------------------删除备份文件------------------')
        shell_cmd = f'rm -rf {self.backup_file}'
        self.log.info(shell_cmd)
        result = self.dbPrimaryDbUser.sh(shell_cmd).result()
        self.log.info(result)
        result = self.dbStandby1DbUser.sh(shell_cmd).result()
        self.log.info(result)
        result = self.dbStandby2DbUser.sh(shell_cmd).result()
        self.log.info(result)
        self.log.info('----------Opengauss_Reliability_Tools_Backup_Case009.py 执行结束-----------------------')
